using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;
using Common;
using Controls;
using SExperiment;
using SProject;
using SSettings;

namespace MainView.StandardCurve
{
	public partial class LoadSubsetStandardCurveWindow : Window, IComponentConnector
	{
		public static readonly DependencyProperty StandardProjectNameProperty = DependencyProperty.Register("StandardProjectName", typeof(List<string>), typeof(LoadSubsetStandardCurveWindow), new UIPropertyMetadata(null));

		public static readonly DependencyProperty SubsetProperty = DependencyProperty.Register("Subset", typeof(Subset), typeof(LoadSubsetStandardCurveWindow), new UIPropertyMetadata(null));

		public static readonly DependencyProperty IsDecryptProperty = DependencyProperty.Register("IsDecrypt", typeof(bool), typeof(LoadSubsetStandardCurveWindow), new UIPropertyMetadata(true));

		private List<ProjectStdCurve> _ProjectStdCurves = new List<ProjectStdCurve>();

		private StandardCurveManage _StandardCurveManage = new StandardCurveManage();

		public List<string> StandardProjectName
		{
			get
			{
				return (List<string>)GetValue(StandardProjectNameProperty);
			}
			set
			{
				SetValue(StandardProjectNameProperty, value);
			}
		}

		public Subset Subset
		{
			get
			{
				return (Subset)GetValue(SubsetProperty);
			}
			set
			{
				SetValue(SubsetProperty, value);
			}
		}

		public bool IsDecrypt
		{
			get
			{
				return (bool)GetValue(IsDecryptProperty);
			}
			set
			{
				SetValue(IsDecryptProperty, value);
			}
		}

		public LoadSubsetStandardCurveWindow()
		{
			_StandardCurveManage.LoadAllStandardCurve();
			InitializeComponent();
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			if (Subset == null)
			{
				return;
			}
			IList<Project> quantitativeProjectList = Subset.GetQuantitativeProjectList();
			foreach (Project item in quantitativeProjectList)
			{
				ProjectStdCurve projectStdCurve = Subset.GetSubsetParamter(item).CurrentProjectStdCurve;
				ProjectStdCurveEx psc = new ProjectStdCurveEx();
				psc.ProjectName = projectStdCurve.ProjectName;
				if (StandardProjectName != null && StandardProjectName.Contains(item.BasicInfo.Name))
				{
					ProjectStdCurve projectStdCurve2 = _StandardCurveManage.Items.Where((ProjectStdCurve s) => Path.GetFileNameWithoutExtension(s.Path) == ConfigSettings.GetInstance().MemorySettings.GetStandardCurver(psc.ProjectName)).FirstOrDefault();
					if (projectStdCurve2 != null && projectStdCurve2.ExternalStdWell != null && ProjectMath(item, (from s in projectStdCurve2.ExternalStdWell
						where s.Project != null
						select s.Project).FirstOrDefault()))
					{
						projectStdCurve = projectStdCurve2.Clone();
						projectStdCurve.External = true;
					}
				}
				psc.External = projectStdCurve.External;
				psc.ExternalStdWell = projectStdCurve.ExternalStdWell;
				psc.ExternalTargetStdCurveList = projectStdCurve.ExternalTargetStdCurveList;
				psc.Path = projectStdCurve.Path;
				psc.Version = projectStdCurve.Version;
				List<string> list = new List<string>();
				foreach (ProjectStdCurve item2 in _StandardCurveManage.Items)
				{
					if (item2.ExternalStdWell != null && ProjectMath(item, (from s in item2.ExternalStdWell
						where s.Project != null
						select s.Project).FirstOrDefault()))
					{
						list.Add(item2.Path);
					}
				}
				psc.PathList = list;
				if (psc.External && !psc.PathList.Contains(psc.Path))
				{
					psc.PathList.Add(psc.Path);
				}
				psc.PropertyChanged += psc_PropertyChanged;
				_ProjectStdCurves.Add(psc);
			}
			dataGrid.ItemsSource = _ProjectStdCurves;
			dataGrid.SelectedIndex = 0;
		}

		private bool ProjectMath(Project p1, Project p2)
		{
			if (p1 == null || p2 == null)
			{
				return false;
			}
			if (p1.BasicInfo.Name != p2.BasicInfo.Name || p1.BasicOption.Items.Count != p2.BasicOption.Items.Count)
			{
				return false;
			}
			for (int i = 0; i < p1.BasicOption.Items.Count; i++)
			{
				if (p1.BasicOption.Items[i].TargetName != p2.BasicOption.Items[i].TargetName || p1.BasicOption.Items[i].ChannelNo != p2.BasicOption.Items[i].ChannelNo || p1.BasicOption.Items[i].TubeNo != p2.BasicOption.Items[i].TubeNo || p1.BasicOption.Items[i].TubeName != p2.BasicOption.Items[i].TubeName)
				{
					return false;
				}
			}
			return true;
		}

		private void psc_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			ProjectStdCurve psc = sender as ProjectStdCurve;
			if (e.PropertyName == "Path" && !string.IsNullOrEmpty(psc.Path))
			{
				ProjectStdCurve projectStdCurve = _StandardCurveManage.Items.Where((ProjectStdCurve s) => s.Path == psc.Path).FirstOrDefault();
				if (projectStdCurve != null)
				{
					projectStdCurve = projectStdCurve.Clone();
					psc.ProjectName = projectStdCurve.ProjectName;
					psc.External = true;
					psc.Path = projectStdCurve.Path;
					psc.ExternalStdWell = projectStdCurve.ExternalStdWell;
					psc.ExternalTargetStdCurveList = projectStdCurve.ExternalTargetStdCurveList;
					psc.Version = projectStdCurve.Version;
				}
			}
		}

		public void SelectItem(object obj)
		{
			dataGrid.SelectedItem = obj;
		}

		private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			RefreshChart();
		}

		private void dataGrid_SourceUpdated(object sender, DataTransferEventArgs e)
		{
			RefreshChart();
		}

		private void RefreshChart()
		{
			gridStdCurve.Children.Clear();
			ProjectStdCurve projectStdCurve = dataGrid.SelectedItem as ProjectStdCurve;
			if (projectStdCurve == null)
			{
				tbVersion.Text = null;
				return;
			}
			tbVersion.Text = projectStdCurve.Version;
			if (projectStdCurve.ExternalTargetStdCurveList == null)
			{
				return;
			}
			base.Cursor = Cursors.Wait;
			if (projectStdCurve.ExternalTargetStdCurveList.Count == 1)
			{
				UCStandardCurveChart uCStandardCurveChart = new UCStandardCurveChart();
				uCStandardCurveChart.TargetStdCurve = projectStdCurve.ExternalTargetStdCurveList[0];
				gridStdCurve.Children.Add(uCStandardCurveChart);
			}
			else
			{
				TabControlEx tabControlEx = new TabControlEx();
				tabControlEx.Style = Utility.GetResource("TabControlStylesmall") as Style;
				foreach (TargetStdCurve externalTargetStdCurve in projectStdCurve.ExternalTargetStdCurveList)
				{
					TabItem tabItem = new TabItem();
					tabItem.Style = Utility.GetResource("TabItemStylesmall") as Style;
					tabItem.Header = externalTargetStdCurve.TargetName;
					tabItem.Content = new UCStandardCurveChart
					{
						TargetStdCurve = externalTargetStdCurve
					};
					tabControlEx.Items.Add(tabItem);
				}
				gridStdCurve.Children.Add(tabControlEx);
			}
			base.Cursor = Cursors.Arrow;
		}

		private void btnOk_Click(object sender, RoutedEventArgs e)
		{
			if (Subset == null)
			{
				return;
			}
			foreach (ProjectStdCurve projectStdCurf in _ProjectStdCurves)
			{
				if (projectStdCurf.External)
				{
					if (projectStdCurf.ExternalTargetStdCurveList == null || projectStdCurf.ExternalTargetStdCurveList.Count == 0)
					{
						MessageBox.Show(Utility.GetMessage("msg_not_std_file"));
						SelectItem(projectStdCurf);
						return;
					}
				}
				else
				{
					projectStdCurf.ExternalStdWell = null;
					projectStdCurf.ExternalTargetStdCurveList = null;
					projectStdCurf.Version = null;
				}
				ProjectStdCurve currentProjectStdCurve = Subset.GetSubsetParamter(projectStdCurf.ProjectName).CurrentProjectStdCurve;
				currentProjectStdCurve.External = projectStdCurf.External;
				currentProjectStdCurve.ExternalTargetStdCurveList = projectStdCurf.ExternalTargetStdCurveList;
				currentProjectStdCurve.ExternalStdWell = projectStdCurf.ExternalStdWell;
				currentProjectStdCurve.Path = projectStdCurf.Path;
				currentProjectStdCurve.Version = projectStdCurf.Version;
				if (currentProjectStdCurve.External)
				{
					ConfigSettings.GetInstance().MemorySettings.AddStandardCurver(projectStdCurf.ProjectName, Path.GetFileNameWithoutExtension(projectStdCurf.Path));
				}
			}
			base.DialogResult = true;
		}

		private void dataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
		{
			if (!IsDecrypt)
			{
				e.Cancel = true;
			}
			if (e.Column.Header.ToString() == Utility.GetMessage("save_file_name"))
			{
				ProjectStdCurve projectStdCurve = e.Row.Item as ProjectStdCurve;
				if (projectStdCurve != null && !projectStdCurve.External)
				{
					e.Cancel = true;
				}
			}
		}
	}
}
